﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Lucandra.Configuration
{
    public class LucandraConfigurationSection : ConfigurationSection
    {
        public const string ConfigurationSectionName = "lucandra.net";

        static LucandraConfigurationSection config = null;

        [ConfigurationProperty("documentsColumnFamily", IsRequired = true)]
        public DocumentsColumnFamilyElement DocumentsColumnFamily
        {
            get { return (DocumentsColumnFamilyElement)this["documentsColumnFamily"]; }
            set { this["documentsColumnFamily"] = value; }
        }

        [ConfigurationProperty("termInfoColumnFamily", IsRequired = true)]
        public TermInfoColumnFamilyElement TermInfoColumnFamily
        {
            get { return (TermInfoColumnFamilyElement)this["termInfoColumnFamily"]; }
            set { this["termInfoColumnFamily"] = value; }
        }

        [ConfigurationProperty("keyspace", DefaultValue = "Lucandra")]
        public string Keyspace
        {
            get { return (string)this["keyspace"]; }
            set { this["keyspace"] = value; }
        }

        [ConfigurationProperty("defaultIndexName", IsRequired = false)]
        public string DefaultIndexName
        {
            get { return (string)this["defaultIndexName"]; }
            set { this["defaultIndexName"] = value; }
        }

        [ConfigurationProperty("defaultAutoCommit", IsRequired = false)]
        public bool DefaultAutoCommit
        {
            get { return (bool)this["defaultAutoCommit"]; }
            set { this["defaultAutoCommit"] = value; }
        }

        [ConfigurationProperty("useKeyspacePerIndex", IsRequired = true)]
        public bool UseKeyspacePerIndex
        {
            get { return (bool)this["useKeyspacePerIndex"]; }
            set { this["useKeyspacePerIndex"] = value; }
        }

        [ConfigurationProperty("caching", IsRequired = true)]
        public CachingElement Caching
        {
            get { return (CachingElement)this["caching"]; }
            set { this["caching"] = value; }
        }

        [ConfigurationProperty("connectionPool", IsRequired = true)]
        public CassandraConnectionPoolElement ConnectionPool
        {
            get { return (CassandraConnectionPoolElement)this["connectionPool"]; }
            set { this["connectionPool"] = value; }
        }

        [ConfigurationProperty("liveSchemaUpdateNode", IsRequired = false)]
        public CassandraEndpointElement LiveSchemaUpdateNode
        {
            get { return (CassandraEndpointElement)this["liveSchemaUpdateNode"]; }
            set { this["liveSchemaUpdateNode"] = value; }
        }

        [ConfigurationProperty("indices", IsRequired = false)]
        public IndexCollection Indexes
        {
            get { return (IndexCollection)this["indices"]; }
            set { this["indices"] = value; }
        }

        public static LucandraConfigurationSection GetConfig()
        {
            if (config == null)
                config = (LucandraConfigurationSection)ConfigurationManager.GetSection(ConfigurationSectionName);
            if (config == null)
                throw new ConfigurationErrorsException("The '" + ConfigurationSectionName + "' configuration section is not defined.");
            return config;
        }

        public static void RefreshConfig()
        {
            ConfigurationManager.RefreshSection(ConfigurationSectionName);
        }
    }
}
